package com.example.petshop.controller;

import com.example.petshop.entity.Cart;
import com.example.petshop.entity.User;
import com.example.petshop.service.CartService;
import com.example.petshop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartsController {

    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/add")
    public boolean addToCart(HttpServletRequest request,
                             @RequestParam("goodsId") Integer goodsId,
                             @RequestParam("goodsName") String goodsName,
                             @RequestParam("description") String description,
                             @RequestParam("images") String images,
                             @RequestParam("price") String price,
                             @RequestParam("shopId") Integer shopId,
                             @RequestParam("quantity") Integer quantity) {
        System.out.println("goodsId: "+goodsId);
        System.out.println("goodsId: " + goodsId);
        System.out.println("goodsName: " + goodsName);
        System.out.println("description: " + description);
        System.out.println("images: " + images);
        System.out.println("price: " + price);
        System.out.println("shopId: " + shopId);
        System.out.println("quantity: " + quantity);
        // 从Session中获取已登录的用户信息
        HttpSession session = request.getSession(false); // false表示不创建新Session
        if (session != null) {
            User loggedInUser = (User) session.getAttribute("loggedInUser");
            if (loggedInUser != null) {
                // 获取登录用户的userId
              Long userId = loggedInUser.getUserId();
                // 添加商品到购物车，并传递userId
                return cartService.addToCart(goodsId, goodsName, description, images, price, shopId, userId,quantity);
            } else {
                // 如果用户未登录，则可以返回一个错误或者不需要执行添加操作
                return false;
            }
        } else {
            // 如果Session为空，则用户可能未登录
            return false;
        }
    }

    @GetMapping("/getall")
    public List<Cart> getAllCarts() {

        return cartService.getAllCarts();

    }
    @GetMapping("/getbyuserid")
    public List<Cart> getCartsByUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("loggedInUser");

        if (user instanceof User) {
            User loggedInUser = (User) user;
            Long userId = loggedInUser.getUserId();
            return cartService.getCartsByUserId(userId);
        } else {
            // 处理未登录用户的情况
            return null;
        }
    }
    @PutMapping("/{cartid}")
    public ResponseEntity<?> updateCartQuantity(@PathVariable Integer cartid,
                                                @RequestBody Cart cartUpdate) {
        System.out.println("cartUpdate:"+cartUpdate);
        // cartUpdate 包含了更新后的 quantity
        boolean result = cartService.updateCartQuantity(cartid, Integer.valueOf(cartUpdate.getQuantity()));
        if (result) {
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    @DeleteMapping("/{cartid}")
    public ResponseEntity<?> deleteCart(@PathVariable Integer cartid) {
        boolean result = cartService.deleteCartById(cartid);
        if (result) {
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/batch")
    public ResponseEntity<?> deleteCarts(@RequestBody List<Integer> cartids) {
        boolean result = cartService.deleteCartsByIds(cartids);
        if (result) {
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
}
